package com.marketapp.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.marketapp.dao.ISectorDao;
import com.marketapp.dao.SectorDaoImpl;
import com.marketapp.domain.Sector;
import com.marketapp.helper.DaoException;
import com.marketapp.helper.ServiceException;
import com.marketapp.util.StringUtil;

public class SectorServiceImpl implements ISectorService {
	
	@Autowired
	ISectorDao sectorDao;
	
	public ISectorDao getSectorDao() {
		return sectorDao;
	}

	public void setSectorDao(ISectorDao sectorDao) {
		this.sectorDao = sectorDao;
	}

	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED,readOnly=true)
	public List<Sector> getAllSector() throws ServiceException {
		List<Sector> sectors;
		try{
			sectors = sectorDao.getAllSector();
		}catch(Exception ex){
			throw new ServiceException("Error:Could not retrieve data.", ex.getCause());
		}
		return sectors;
	}
	
	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED,readOnly=true)
	public Sector getSectorById(int sectorId) throws ServiceException {
		try {
			return sectorDao.getSectorById(sectorId);
		} catch (DaoException e) {
			throw new ServiceException("Error:Could not get data",e.getCause());
		}
	}

	@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED)
	public void saveSector(Sector sector) throws ServiceException {
		try {
			synchronized (sector) {
				sector.setSectorName(StringUtil.getStringUpperCase(sector.getSectorName().trim()));
				sectorDao.saveSector(sector);
			}
		} catch (DaoException e) {
			throw new ServiceException("Error:Could not save data",e.getCause());
		}
	}

	@Override
	public boolean checkifSectorExist(String sector_name)
			throws ServiceException {
		List<Sector> sectors;
		try {
			sectors=sectorDao.getSectorByName(sector_name);
			return sectors.isEmpty()?false:true;
		} catch (DaoException e) {
			// TODO Auto-generated catch block
			throw new ServiceException("Error:Could not get data",e.getCause());
		}
	}

	@Override
	public List<Sector> getSectorByName(String sector_name)
			throws ServiceException {
		List<Sector> sectors;
		try {
			sectors=sectorDao.getSectorByName(sector_name);
			return sectors;
		} catch (DaoException e) {
			throw new ServiceException("Error:Could not get data",e.getCause());
		}
	}

	@Override
	public List<String> searchSectorByName(String sector_name)
			throws ServiceException {
		List<Sector> sectors;
		List<String> searchResultSector = new ArrayList<String>();
		try {
			sectors=sectorDao.searchSectorByName(sector_name);
			for(Sector s:sectors){
				searchResultSector.add(s.getSectorName());
			}
			return searchResultSector;
			
		} catch (DaoException e) {
			throw new ServiceException("Error:Could not get data",e.getCause());
		}
	}

}
